package pub.elephants.serializable;

import pub.elephants.serializable.impl.HessianSerializer;

import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 序列化入口
 */
public abstract class ElephantsSerializable {
    /**
     * serializable
     *
     * @param obj
     * @param <T>
     * @return
     */
    public abstract <T> byte[] serializable(T obj) throws IOException;

    /**
     * un serializable
     *
     * @param <T>
     * @param bytes
     * @param clazz
     * @return
     */
    public abstract <T> T unSerializable(byte[] bytes, Class<T> clazz) throws IOException;

    public enum SerializableTypeEmum {
        HESSIAN(new HessianSerializer()),;


        public final ElephantsSerializable serializer;

        private SerializableTypeEmum(ElephantsSerializable serializer) {
            this.serializer = serializer;
        }

        public static SerializableTypeEmum match(String name, SerializableTypeEmum defaultSerializer) {
            for (SerializableTypeEmum item : SerializableTypeEmum.values()) {
                if (item.name().equals(name)) {
                    return item;
                }
            }
            return defaultSerializer;
        }
    }
    public static void main(String[] args)throws IOException {
        ElephantsSerializable elephantsSerializable=SerializableTypeEmum.match("HESSIAN",null).serializer;
        StringBuffer sb=new StringBuffer("xxx");
        Cat cat=new Cat(sb.toString(),3);
        byte[] bytes=elephantsSerializable.serializable(cat);
        System.out.println(bytes);
        System.out.println(bytes.length);
        System.out.println(elephantsSerializable.unSerializable(bytes,Cat.class).getName());
    }



}
 class Cat implements Serializable  {

    private String name;
    private int age;

    public Cat() {
    }

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
